package com.myzl.web.controller.coal;

import com.myzl.coal.domain.RowData;
import com.myzl.coal.service.IRowDataService;
import com.myzl.common.annotation.Log;
import com.myzl.common.core.controller.BaseController;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.core.page.TableDataInfo;
import com.myzl.common.enums.BusinessType;
import com.myzl.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 每天每列统计Controller
 *
 * @author myzl
 * @date 2023-03-28
 */
@RestController
@RequestMapping("/coal/rowData")
public class RowDataController extends BaseController
{
    @Autowired
    private IRowDataService rowDataService;

    /**
     * 查询每天每列统计列表
     */
    @PreAuthorize("@ss.hasPermi('coal:rowData:list')")
    @GetMapping("/list")
    public TableDataInfo list(RowData rowData)
    {
        startPage();
        List<RowData> list = rowDataService.selectRowDataList(rowData);
        return getDataTable(list);
    }

    /**
     * 查询每天每列统计列表
     */
    @PreAuthorize("@ss.hasPermi('coal:rowData:list')")
    @GetMapping("/allList")
    public AjaxResult allList(RowData rowData)
    {
        List<RowData> list = rowDataService.selectRowDataList(rowData);
        List<RowData> avgDensity = rowDataService.selectAvgDensity();
        return success(list).put("avgDensity",avgDensity);
    }

    /**
     * 导出每天每列统计列表
     */
    @PreAuthorize("@ss.hasPermi('coal:rowData:export')")
    @Log(title = "每天每列统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RowData rowData)
    {
        List<RowData> list = rowDataService.selectRowDataList(rowData);
        ExcelUtil<RowData> util = new ExcelUtil<RowData>(RowData.class);
        util.exportExcel(response, list, "每天每列统计数据");
    }

    /**
     * 获取每天每列统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('coal:rowData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(rowDataService.selectRowDataById(id));
    }

    /**
     * 新增每天每列统计
     */
    @PreAuthorize("@ss.hasPermi('coal:rowData:add')")
    @Log(title = "每天每列统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RowData rowData)
    {
        return toAjax(rowDataService.insertRowData(rowData));
    }

    /**
     * 修改每天每列统计
     */
    @PreAuthorize("@ss.hasPermi('coal:rowData:edit')")
    @Log(title = "每天每列统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RowData rowData)
    {
        return toAjax(rowDataService.updateRowData(rowData));
    }

    /**
     * 删除每天每列统计
     */
    @PreAuthorize("@ss.hasPermi('coal:rowData:remove')")
    @Log(title = "每天每列统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rowDataService.deleteRowDataByIds(ids));
    }

    @Log(title = "保存数据", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadData")
    public AjaxResult uploadData(@RequestBody Map<String,String> data){
        return success(rowDataService.updateBasisData(data));
    }

    @GetMapping("/avgDensity")
    public AjaxResult getAvgDensity(){
        List<RowData> avgDensity = rowDataService.selectAvgDensity();
        return success(avgDensity);
    }
}
